/**
 * 
 */
package info.niwota.start;

import info.niwota.start.contact.Contact;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author qiangli
 *
 */
public class RedialActivity extends Activity implements OnClickListener {

	private static final String TAG = "RedialActivity";
	
	private static final int RC_CALL = 1;

	private List<Contact> list = new ArrayList<Contact>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
		//
		super.onCreate(savedInstanceState);
	
		setContentView(R.layout.contact_main);

		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_redial);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onNewIntent " + intent);
		}
		super.onNewIntent(intent);

		setIntent(intent);
	}

	@Override
	protected void onPause() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onPause");
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onResume");
		}
		super.onResume();

		initUi();
	}

	@Override
	protected void onDestroy() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onDestroy");
		}
		super.onDestroy();
	}

	@Override
	protected void onStart() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onStart");
		}
		super.onStart();
	}

	@Override
	protected void onStop() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onStop");
		}
		super.onStop();
	}

	private void initUi() {
		final Context ctx = this;
		try {
			
			final int[] ids = new int[] { R.id.contact1, R.id.contact2,
					R.id.contact3, R.id.contact4, R.id.contact5 };
			final int size = ids.length;
			final int max = Preferences.getRedialMax(ctx);
			
			list.clear();
			if (max > 0) {
				CallLogs.load(ctx, list, max);
			}
			final int cnt = list.size();
			for (int i = 0; i < size; i++) {
				View v = findViewById(ids[i]);
				if (i < cnt) {
					final Contact c = list.get(i);
					setText(v, R.id.contact_name, c.name);
					setText(v, R.id.contact_phone, c.phone);
					setText(v, R.id.contact_note, c.note);
					v.setVisibility(View.VISIBLE);
					v.setOnClickListener(this);
				} else {
					v.setVisibility(View.GONE);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setText(View v, int id, String text) {
		TextView f = (TextView) v.findViewById(id);
		f.setText(text);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onActivityResult " + data);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onOptionsItemSelected ");
		}
		return false;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return false;
	}

	private void quit() {
		finish();
		
		Intent i = new Intent(Intents.ACTION_POWERUP_QUIT);
		sendOrderedBroadcast(i, null);
	}

	private void doCall(String phone) {

		try {
			Activity ctx = this;

			if (!TextUtils.isEmpty(phone)) {
				Intent i = new Intent(Intent.ACTION_CALL, Uri.fromParts("tel",
						phone, null));
				ctx.startActivityForResult(i, RC_CALL);
			}
		} catch (Exception e) {
			if (Debug.DEBUG) {
				e.printStackTrace();
			}
			showError(e);
		}
	}

	private void showError(Exception e) {
		Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onKeyDown key: " + keyCode);
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			quit();
			return true;
		}
		return true;
	}

	public void onClick(View v) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onClick " + v);
		}

		//
		String phone = ((TextView) v.findViewById(R.id.contact_phone))
				.getText().toString();
		if (Debug.DEBUG) {
			Log.d(TAG, "onClick: " + phone);
		}
		//
		doCall(phone);
	}

}
